package xyz.penghaonan.p2plib.job.operations;

import xyz.penghaonan.p2plib.P2PManager;
import xyz.penghaonan.p2plib.job.base.BaseOperation;
import xyz.penghaonan.p2plib.helper.service.ServiceHelper;

public class StopServiceOperation extends BaseOperation {
    public StopServiceOperation() {
    }

    @Override
    public void doOperation() {
        notifyProgress(0.2f, "结束守护服务");
        if (P2PManager.getServiceHelper().isServiceEnable()) {
            ServiceHelper.P2PServiceStateListener listener = new ServiceHelper.P2PServiceStateListener() {
                @Override
                public void onStateChanged(boolean enable) {
                    operationContinue();
                }
            };
            P2PManager.getServiceHelper().addStateListener(listener);
            P2PManager.getServiceHelper().stop();
            operationWait();
            P2PManager.getServiceHelper().removeStateListener(listener);
            setResult(Result.result(!P2PManager.getServiceHelper().isServiceEnable(), !P2PManager.getServiceHelper().isServiceEnable() ? "Stop service success" : "Stop service failed"));
        }
        notifyProgress(1f, "守护服务结束成功");
    }

    @Override
    public String getName() {
        return "停止守护服务";
    }
}
